package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SysUserSyTypeBo;
import com.ruoyi.system.domain.vo.SysUserSyTypeVo;
import com.ruoyi.system.domain.SysUserSyType;
import com.ruoyi.system.mapper.SysUserSyTypeMapper;
import com.ruoyi.system.service.ISysUserSyTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户试验类型范围Service业务层处理
 *
 * @author zyb
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class SysUserSyTypeServiceImpl implements ISysUserSyTypeService {

    private final SysUserSyTypeMapper baseMapper;

    /**
     * 查询用户试验类型范围
     */
    @Override
    public SysUserSyTypeVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户试验类型范围列表
     */
    @Override
    public TableDataInfo<SysUserSyTypeVo> queryPageList(SysUserSyTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysUserSyType> lqw = buildQueryWrapper(bo);
        Page<SysUserSyTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户试验类型范围列表
     */
    @Override
    public List<SysUserSyTypeVo> queryList(SysUserSyTypeBo bo) {
        LambdaQueryWrapper<SysUserSyType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysUserSyType> buildQueryWrapper(SysUserSyTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysUserSyType> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, SysUserSyType::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getSyTypeValue()), SysUserSyType::getSyTypeValue, bo.getSyTypeValue());
        return lqw;
    }

    /**
     * 新增用户试验类型范围
     */
    @Override
    public Boolean insertByBo(SysUserSyTypeBo bo) {
        SysUserSyType add = BeanUtil.toBean(bo, SysUserSyType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户试验类型范围
     */
    @Override
    public Boolean updateByBo(SysUserSyTypeBo bo) {
        SysUserSyType update = BeanUtil.toBean(bo, SysUserSyType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysUserSyType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户试验类型范围
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
